<template>
    <div class="loginBody">
        <div class="loginDiv">
            <div class="login-content">
                <h1 class="login-title">库存管理系统</h1>
                <el-form :model="loginForm" label-width="100px"
                         :rules="rules" ref="loginForm">
                    <el-form-item label="账号" prop="loginName">
                        <el-input style="width: 200px" type="text" v-model="loginForm.loginName"
                                  autocomplete="off" size="small"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input style="width: 200px" type="password" v-model="loginForm.password"
                                  show-password autocomplete="off" size="small" @keyup.enter.native="confirm"></el-input>
                    </el-form-item>
                    <el-form-item class="login-title" >
                        <el-button type="primary" @click="confirm" :disabled="confirm_disabled" v-loading.fullscreen.lock="loading" style="margin-right: 100px;">确 定</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>


<script>
    export default {
        name: "Login",
        data(){
            return{
                confirm_disabled:false,
                loading: false,
                jwt:'',
                loginForm:{
                  loginName:'',
                  password:''
                },
                rules:{
                  loginName: [
                    { required: true, message: '请输入账号', trigger: 'blur' }
                  ],
                  password: [
                    { required: true, message: '请输密码', trigger: 'blur' }
                  ],
                }
            }
        },
        methods:{
            confirm(){
                this.confirm_disabled=true;
                this.loading = true;
                  this.$refs.loginForm.validate((valid) => {
                    if (valid) { //valid成功为true，失败为false
                      //去后台验证用户名密码
                      this.$axios.post('/zzh-staff/login',this.loginForm).then(res=>res.data).then(res=>{
                        if(res.resultCode==200){
                          //localStorage.setItem("jwt",JSON.stringify(res.data.jwt))
                          //session存储
                          sessionStorage.setItem("user",JSON.stringify(res.data.user))
                          this.$store.commit("setToken",res.data.jwt)
                          this.menuList();
                          this.loading = false;
                          // 收到登录成功的响应，取消定时器，执行页面跳转到主页的操作
                          clearTimeout(loginTimer);
                          //跳转到主页
                          this.$router.replace('/Index');
                        }else{
                          this.confirm_disabled=false;
                          this.$message({
                            showClose: true,
                            message: '校验失败，用户名或密码错误！',
                            type: 'error'
                          });
                          clearTimeout(loginTimer);
                          return false;
                        }
                      });
                    } else {
                      this.confirm_disabled=false;
                      console.log('校验失败');
                      return false;
                    }
                  });
              // 设置定时器，在一定时间内没有收到登录成功的响应时，执行页面跳转到登录页面的操作
              const loginTimer = setTimeout(() => {
                this.confirm_disabled=false;
              }, 10000); // 设置超时时间为10秒
            },
          menuList(){
            this.$axios.post('/zzh-function/list').then(res=>res.data).then(res=>{
              if(res.resultCode==200){
                this.$store.commit("setMenu",res.data)
              }else{
                this.confirm_disabled=false;
                alert('菜单设置失败！');
                return false;
              }
            });

          }
        }
    }
</script>

<style scoped>
    .loginBody {
        position: absolute;
        width: 100%;
        height: 100%;
        background-color: #ACD6FF;
    }
    .loginDiv {
        position: absolute;
        top: 50%;
        left: 50%;
        margin-top: -200px;
        margin-left: -250px;
        width: 450px;
        height: 330px;
        background: #fff;
        border-radius: 5%;

    }
    .login-title {
        margin: 20px 0;
        text-align: center;
    }
    .login-content {
        width: 400px;
        height: 250px;
        position: absolute;
        top: 25px;
        left: 25px;
    }
</style>